package com.op.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.op.aspect.MailNotice;
import com.op.aspect.MailTempAccount;
import com.op.entity.User;
import com.op.model.vo.UserInfoVO;
import com.op.response.ResponseResult;
import com.op.service.JWTService;
import com.op.service.UserService;
import io.minio.errors.MinioException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author 孤焰
 * @since 2021-09-28 19:53:54
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private JWTService jwtService;

    /**
     * 登录
     * @param username 用户名或邮箱
     * @param password 密码
     * @return Token字符串
     */
    @PostMapping("/login")
    public ResponseResult<String> login(String username, String password) throws Exception {
        return ResponseResult.success(jwtService.create(userService.login(username, password)));
    }

    /**
     * 修改密码
     * @param user 修改用户实体
     * @return 修改结果
     */
    @PutMapping("/changePassword")
    public ResponseResult<Boolean> changePassword(User user) {
        return ResponseResult.success(userService.changePassword(user));
    }

    /**
     * 将token解码
     * @param token token加密字符串
     * @return token解码后包含（id,username,avatarPath）
     * @throws Exception
     */
    @GetMapping("/decode")
    public ResponseResult<User> decode(String token) throws Exception {
        return ResponseResult.success(jwtService.deCode(token));
    }

    /**
     * 分页查询所有数据
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public ResponseResult<Page<UserInfoVO>> page(Page<UserInfoVO> page, User user) {
        return ResponseResult.success(userService.page(page, user));
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public ResponseResult<User> selectOne(@PathVariable("id") Long id) {
        return ResponseResult.success(userService.getById(id));
    }

    /**
     * 查询未审批的用户个数
     * @return 未审批的用户个数
     */
    @GetMapping("/notReview")
    public ResponseResult<Integer> notReview() {
        return ResponseResult.success(userService.notReview());
    }

    /**
     * 检查value是否合法
     * @param id 主键id
     * @param value 待检查字符串
     * @return 检查结果
     */
    @GetMapping("/check")
    public ResponseResult<Boolean> check(Long id, String value) {
        return ResponseResult.success(userService.check(id, value));
    }

    /**
     * 新增数据
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public ResponseResult<Boolean> insert(User user) {
        return ResponseResult.success(userService.save(user));
    }

    /**
     * 修改数据
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> update(User user) {
        return ResponseResult.success(userService.updateById(user));
    }

    /**
     * 添加或修改数据
     * @param user 实体对象
     * @param avatar 头像文件
     * @return 添加或修改结果
     * @throws MinioException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     */
    @PostMapping("/saveOrUpdate")
    public ResponseResult<Boolean> saveOrUpdate(User user, MultipartFile avatar) throws MinioException, NoSuchAlgorithmException, InvalidKeyException, IOException {
        return ResponseResult.success(userService.saveOrUpdate(user, avatar));
    }

    /**
     * 向redis中注册临时用户
     * @param user 实体对象
     * @return 注册结果
     */
    @MailTempAccount
    @PostMapping("/tempAccount")
    public ResponseResult<Boolean> registerTempAccount(User user) {
        return ResponseResult.success(userService.registerTempAccount(user));
    }

    /**
     * 逻辑删除数据
     * @param idList 主键集合
     * @return 删除结果
     */
    @PutMapping("/batchDelete")
    public ResponseResult<Boolean> batchLogicDelete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.success(userService.logicDelBatchById(idList));
    }

    /**
     * 批量删除数据
     * @param idList 主键集合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    public ResponseResult<Boolean> batchDelete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.success(userService.removeByIds(idList));
    }

    /**
     * 账号通过审核
     * @param user
     * @return
     */
    @MailNotice
    @PutMapping("/auditAccount")
    public ResponseResult<Boolean> auditAccount(User user) {
        return ResponseResult.success(userService.updateById(user));
    }
}

